package ds.stack;

public class StackX {

	private int maxSize;  // size of stack array
	private long[] stackArray;
	private int top;      // top of stack
	// ----------------------------------------
	public StackX(int maxSize) {   // contructor
		this.maxSize = maxSize;    // set array size
		stackArray = new long[maxSize];  // create array
		top = -1;                  // no items yet
	}

	public void push(long value) throws Exception{
		if(!isFull())
			stackArray[++top]=value;
		else
			throw new Exception("堆栈已满，无法继续push");
	}
	
	public long pop() throws Exception{
		if(!isEmpty())
			return stackArray[top--];
		else
			throw new Exception("空栈无法继续pop");
	}
	
	public long peek() throws Exception{
		if(!isEmpty())
			return stackArray[top];
		else
			throw new Exception("空栈无法peek");
	}
	
	public boolean isEmpty() {
		return top==-1;
	}
	
	public boolean isFull() {
		return top == maxSize - 1;
	}
	
	public static void main(String[] args) throws Exception{
		StackX s = new StackX(10);
		s.push(5);
		s.push(100);
		s.push(100);
		s.push(100);
		s.push(700);
		
		for(;!s.isEmpty();)
		{
			System.out.println(s.pop());
		}
	}

}
